package com.jbp.service.impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.jbp.exception.RRException;
import com.jbp.service.PropertyService;
import com.jbp.service.SendNoteMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Set;

@Service
public class SendNoteMessageServiceYtxmpl implements SendNoteMessageService {
    private static final Logger logger = LoggerFactory.getLogger(SendNoteMessageServiceYtxmpl.class);

    @Autowired
    private PropertyService propertyService;

    @Override
    public boolean send(String phone, String code) {
        try {
            // 服务地址
            String serverIp = propertyService.getV("msg_ytx_server_ip");
            // 请求端口
            String serverPort = propertyService.getV("msg_ytx_server_port");
            // 主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
            String accountSId = propertyService.getV("msg_ytx_account_sid");
            String accountToken = propertyService.getV("msg_ytx_account_token");
            // 请使用管理控制台中已创建应用的APPID
            String appId = propertyService.getV("msg_ytx_app_id");
            // 短信模板id
            String templateId = propertyService.getV("msg_ytx_template_code");

            // 设置超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            CCPRestSmsSDK sdk = new CCPRestSmsSDK();
            sdk.init(serverIp, serverPort);
            sdk.setAccount(accountSId, accountToken);
            sdk.setAppId(appId);
            sdk.setBodyType(BodyType.Type_JSON);

            String[] datas = {code};

            // String subAppend="1234";  //可选 扩展码，四位数字 0~9999
            // String reqId="fadfafasd";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
            // HashMap<String, Object> result = sdk.sendTemplateSMS(phone,templateId,datas,subAppend,reqId);

            // 测试异常处理
            //throw new RuntimeException("test stop");

            HashMap<String, Object> result = sdk.sendTemplateSMS(phone, templateId, datas);

            if ("000000".equals(result.get("statusCode"))) {
                //正常返回输出data包体信息（map）
                HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
                Set<String> keySet = data.keySet();
                for (String key : keySet) {
                    Object object = data.get(key);
                    logger.debug(String.format("给%s发送短信成功, 接口返回信息: %s = %s", phone, key, object));
                }
            } else {
                //异常返回输出错误码和错误信息
                logger.error(String.format("给%s发送短信失败，错误码=%s，错误信息=%s", phone, result.get("statusCode"),
                        result.get("statusMsg")));
                return false;
            }
        } catch (Exception ex) {
            logger.error(String.format("验证码发送失败，mobile: %s", phone), ex);
            throw new RRException("发送失败");
        }

        return true;
    }
}
